package com.zmn.oms.dubbo.impl.normal.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.work.tag.OrderTagBService;
import com.zmn.oms.common.constant.OrderTagConsts;
import com.zmn.oms.dubbo.dto.common.work.OrderTagAddDIO;
import com.zmn.oms.dubbo.dto.common.work.OrderTagCheckDIO;
import com.zmn.oms.dubbo.interfaces.normal.order.OrderTagRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.tag.OrderTagAddDTO;
import com.zmn.oms.services.interfaces.tag.OrderTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述：工单标签服务
 *
 * @author ligang
 * @date 2021/11/22 11:03
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderTagRemoteServiceImpl implements OrderTagRemoteService {

    @Autowired
    private OrderTagBService orderTagBService;
    @Autowired
    private OrderTagService orderTagService;

    /**
     * 添加工单标记
     *
     * @param orderTagAddDIO
     * @return
     */
    @Override
    public ResponseDTO addTag(OrderTagAddDIO orderTagAddDIO) {
        log.info("添加工单标记接口入参：【{}】", JSON.toJSONString(orderTagAddDIO));
        try {
            OrderTagAddDTO orderTagAddDTO = BeanMapper.map(orderTagAddDIO, OrderTagAddDTO.class);
            orderTagBService.addOrderTag(orderTagAddDTO);
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("添加工单标记接口失败：{}", e);
            return ResponseDTO.fail(e.getMessage());
        }
    }

    /**
     * 检查工单是否有指定标记
     *
     * @param orderTagAddDIO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> tagExistCheck(OrderTagCheckDIO orderTagAddDIO) {
        log.info("添加工单标记接口入参：【{}】", JSON.toJSONString(orderTagAddDIO));
        try {
            Integer count = orderTagService.countOrderTagByOrderIdAndTagId(orderTagAddDIO.getOrderId(), orderTagAddDIO.getTagId());
            return ResponseDTO.success(count > 0);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("检查工单是否有指定标记失败：{}", e);
            return ResponseDTO.fail(e.getMessage());
        }
    }

    @Override
    public ResponseDTO<Boolean> existsExcludePerformanceCategoryTag(Long orderId, Long workId) {
        log.info("OrderTagRemoteServiceImpl#existsExcludePerformanceCategoryTag 检查工单排除工程师绩效标签接口入参：【orderId：{}，workId：{}】",
                JSON.toJSONString(orderId),
                JSON.toJSONString(workId));
        try {
            Boolean existsTag = orderTagBService.existsCategoryTagByCategoryId(orderId, workId, OrderTagConsts.TAG_CATEGORY_ID_EXCLUDE_PERFORMANCE);
            return ResponseDTO.success(existsTag);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("检查工单标记接口失败：{}", e);
            return ResponseDTO.fail(e.getMessage());
        }
    }

    /**
     * 指定时间内， 派给工程师的同用户的订单中，是否有有效联系，排除当前订单
     * @param orderId
     * @return
     */
    @Override
    public ResponseDTO<Boolean> existContactEffectiveTagOrderByExcludeOrderId(Long orderId, Long workId) {
        log.info("existContactEffectiveTagOrderByExcludeOrderId：【{}】", orderId);
        try {
            boolean exist = orderTagBService.existContactEffectiveTagOrderByExcludeOrderId(orderId, workId);
            return ResponseDTO.success(exist);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("existContactEffectiveTagByOrderId：{}", e);
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
